package cn.ffcs.uec.wBStaffInfo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecNationdutyManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecNationduty;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbNationdutyListboxBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:职业资格管理 CRUD插件
 * @Description:
 * @Company: FFCS
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class WbNationdutyMainComposer extends BasePortletComposer implements
IPortletInfoProvider {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecStaffInfo=new UecOutStaffInfo();
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private WbNationdutyListboxBean bean = new WbNationdutyListboxBean();
	private String editNationdutyUrl="/pages_nmg/uecOutStaffInfomation/wb_nationduty_edit.zul";

	/**
	 * 职业资格管理实体
	 */
	private UecNationduty uecNationduty;
	/**
	 * 翻页查询条件实体
	 */
	private UecNationduty nationdutyPaging;

	/**
	 * 职业资格管理查询实体
	 */

	private UecNationdutyManager uecNationdutyManager = (UecNationdutyManager) ApplicationContextUtil
			.getBean("uecNationdutyManager");
	private UecOutStaffManager uecOutStaffInfoManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	private List<UecNationduty> nationdutyList = new ArrayList<UecNationduty>();
	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	/**
	 * 是否由外包入职申请页引用
	 */
	private boolean isOutStaffEntryExt = false;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 */
	public void onCreate$wbNationdutyListWin() {
		//this.setButtonValid(false, false, false, false);
		onUecOutStaffNationdutyQueryResponse();
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecNationdutyList() throws Exception {
		if (this.bean.getUecNationdutyListbox().getSelectedCount() > 0) {
			uecNationduty = (UecNationduty) bean.getUecNationdutyListbox()
					.getSelectedItem().getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffNationdutyQueryResponse() {
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			uecStaffInfo.setPsnCode(screenName.toUpperCase());
		}
		if(uecStaffInfo!=null) {
			uecStaffInfo=uecOutStaffInfoManager.findUecOutStaffInfo(uecStaffInfo);
		}
		if (null != uecStaffInfo) {
	        //设置操作按钮，非盟市主管不可操作按钮
	       /* if(PermissionUtil.isNoMainOutStaff()) {
	            this.setButtonValid(false, true, true, true);
	        }else {
	            this.setButtonValid(true, true, true, false);
	        }*/
			uecNationduty = new UecNationduty();
			uecNationduty.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecNationduty.setStaffId(uecStaffInfo.getOutStaffId());
			nationdutyPaging = uecNationduty;
			this.onQueryUecNationduty();
		}
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecNationduty() {
		this.bean.getUecNationdutyListboxPaging().setActivePage(0);
		this.onQueryUecNationdutyList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecNationduty() {
		ListboxUtils.clearListbox(this.bean.getUecNationdutyListbox());
		this.bean.getUecNationdutyListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecNationduty() throws Exception, InterruptedException {
		if (uecNationduty != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecNationduty", uecNationduty);
			//map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					this.editNationdutyUrl,null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecNationduty() throws Exception {
		if (null != uecStaffInfo) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecStaffInfo);
			//map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					this.editNationdutyUrl,null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecNationduty = (UecNationduty) event.getData();
						nationdutyList.add(uecNationduty);
						if (!isOutStaffEntryExt) {
							onQueryUecNationdutyList();
						} else {
							ListModel list = new BindingListModelList(
									nationdutyList, true);
							bean.getUecNationdutyListbox().setModel(list);
							bean.getUecNationdutyListboxPaging()
									.setTotalSize(1);
						}

					}
				}
			});
		}
	}


	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecNationduty() throws Exception, InterruptedException {
		if (null != uecNationduty) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecNationduty", uecNationduty);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					this.editNationdutyUrl,null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						if (isOutStaffEntryExt) {
							nationdutyList.remove(uecNationduty);
						}
						uecNationduty = (UecNationduty) event.getData();
						// 新增或修改完成后，列表从数据库更新
						if (!isOutStaffEntryExt) {
							onQueryUecNationdutyList();
						} else {
							nationdutyList.add(uecNationduty);
							ListModel list = new BindingListModelList(
									nationdutyList, true);
							bean.getUecNationdutyListbox().setModel(list);
							bean.getUecNationdutyListboxPaging()
									.setTotalSize(1);

						}

					}
				}
			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}


	/**
	 * 删除按钮
	 */
	public void onDelUecNationduty() {
		if (this.uecNationduty != null && uecNationduty.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecNationduty.removeOnly();
						PubUtil.reDisplayListbox(
								bean.getUecNationdutyListbox(), uecNationduty,
								"del");
						uecNationduty = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutNationdutyListboxPaging() {
		this.onQueryUecNationdutyList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecNationduty = null;
		nationdutyPaging = null;
		nationdutyList.clear();
	}

	/**
	 * 查询
	 */
	public void onQueryUecNationdutyList() {
		if (null != nationdutyPaging) {
			PageInfo pageInfo = uecNationdutyManager
					.queryPageInfoByUecNationduty(nationdutyPaging,
							this.bean.getUecNationdutyListboxPaging()
									.getActivePage() + 1, this.bean
									.getUecNationdutyListboxPaging()
									.getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecNationdutyListbox().setModel(list);
			this.bean.getUecNationdutyListboxPaging().setTotalSize(
					pageInfo.getTotalCount());

		}

}
	
	/**
	 * 判断是否入职
	 * 
	 * @return
	 */
	private boolean isOnTrial() {
		UecTrial qryUecTrial = UecTrial.newInstance();
		qryUecTrial.setStaffId(uecStaffInfo.getOutStaffId());
		qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
		if (null == qryUecTrial) {
			ZkUtil.showError("您未办理入职,请主管执行入职操作", "温馨提示");
			return false;
		}
		return true;
	}
}